package com.cloudkinto.controller.regular;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.warehouse.CompanyWarehouseVo;
import com.cloudkinto.service.channel.LogisticsChannelService;
import com.cloudkinto.service.warehouse.WarehouseService;
import com.cloudkinto.vo.channel.lc.LcOptionRes;
import com.cloudkinto.vo.regular.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.regular.PlatformOrderRegularService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-11-28
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "平台订单规则模块")
@RequestMapping("/api/admin/oms/PlatformOrderRegular")
public class PlatformOrderRegularOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(PlatformOrderRegularOmsController.class);

    private final PlatformOrderRegularService service;
    private final LogisticsChannelService logisticsChannelService;
    private final WarehouseService warehouseService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "新增", notes = "修改")
    public SingleResult add(@RequestBody @Validated PlatRegularAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功", service.add(req, userId, getCompanyId()));
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated PlatRegularUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId, getCompanyId()));
    }

    @UserSessionRequired
    @DeleteMapping("/deleteRegular")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult deleteRegular(@RequestBody @Validated PlatRegularSortReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(req.getIds(), userId, getCompanyId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatRegularUpdateReq.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatRegularPageDto.class))
    @ApiImplicitParams({@ApiImplicitParam(name = "regularName", value = "规则名称"),
            @ApiImplicitParam(name = "status", value = "状态"),
    })
    public SingleResult list(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, Object> map = new HashMap<>();
        String regularName = request.getParameter("regularName");
        String status = request.getParameter("status");
        if (companyId != null) {
            map.put("companyId", companyId);
        }
        map.put("status", status);
        map.put("regularName", regularName);

        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = PlatRegularPageDto.class))
    @ApiImplicitParams({@ApiImplicitParam(name = "regularName", value = "规则名称"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    public PageResult listPage(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, Object> map = new HashMap<>();
        String regularName = request.getParameter("regularName");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String status = request.getParameter("status");
        if (companyId != null) {
            map.put("companyId", companyId);
        }
        map.put("status", status);
        map.put("regularName", regularName);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    @UserSessionRequired
    @GetMapping("/getLcForRegular")
    @ApiImplicitParams({@ApiImplicitParam(name = "warehouseId", value = "仓库id")})
    @ApiOperation(value = "根据仓库id 获取物流渠道", notes = "根据仓库id 获取物流渠道")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = LcOptionRes.class))
    public SingleResult getLcForOut(Long warehouseId) {
        return SingleResult.success("查询成功",logisticsChannelService.getLcForChoice(warehouseId, getCompanyId(), getTenantId()));
    }


    @UserSessionRequired
    @GetMapping("/getWarehouseCompany")
    @ApiOperation(value = " 获取公司可用仓库列表", notes = "获取公司可用仓库列表")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CompanyWarehouseVo.class))
    public SingleResult getWarehouseCompany() {
        return SingleResult.success("查询成功",warehouseService.getWarehouseCompany(getCompanyId(), null));
    }

    @UserSessionRequired
    @PostMapping("/updateSort")
    @ApiOperation(value = "排序", notes = "排序")
    public SingleResult updateSort(@RequestBody @Validated PlatRegularSortReq req) {
        Long userId = getCurrentUserId();
        service.updateSort(req, userId, getCompanyId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @PostMapping("/updateRegularStatus")
    @ApiOperation(value = "更新状态", notes = "更新状态")
    public SingleResult updateRegularStatus(@RequestBody @Validated PlatRegularStatusReq req) {
        Long userId = getCurrentUserId();
        service.updateRegularStatus(req, userId, getCompanyId());
        return SingleResult.success();
    }

//    @UserSessionRequired
    @GetMapping("/testToJinYunApi")
    @ApiOperation(value = "test", notes = "test")
    public SingleResult testToJinYunApi() throws Exception{
        return SingleResult.success("查询成功",  service.testToJinYunApi());
    }


}

